package com.lingjtx.common.captcha;

import java.lang.reflect.Constructor;

public enum CaptchaType {

    VERIFY_CODE("verifyCode", "验证码", VerifyCode.class),

    /**
     * 旋转拼图.
     */
    ROTATE_PUZZLE("rotatePuzzle", "旋转拼图", RotatePuzzleCaptcha.class),
    /**
     * 滑块拼图
     */
    BLOCK_PUZZLE("blockPuzzle", "滑块拼图", BlockPuzzleCaptcha.class),
    /**
     * 文字点选.
     */
    CLICK_WORD("clickWord", "文字点选", ClickWordCaptcha.class),

    ;

    private String value;

    private String desc;

    private Class<CaptchaHandle> clazz;

    CaptchaType(String value, String desc, Class clazz) {
        this.value = value;
        this.desc = desc;
        this.clazz = clazz;
    }

    public String getValue() {
        return value;
    }

    public String getDesc() {
        return desc;
    }

    public Class<CaptchaHandle> getClazz() {
        return clazz;
    }

    public static CaptchaHandle getObject(CaptchaType type) {
        try {
            Constructor<CaptchaHandle> constructor = type.clazz.getConstructor();
            return constructor.newInstance();
        } catch (Exception e) {
            return null;
        }
    }

    //根据codeValue获取枚举
    public static CaptchaType parseFromValue(String value) {
        for (CaptchaType e : CaptchaType.values()) {
            if (e.value.equals(value)) {
                return e;
            }
        }
        return null;
    }

    @Override
    public String toString() {
        return this.getValue();
    }
}
